package com.yzzrjy.controller.mainservlet.register;

import com.alibaba.fastjson.JSONObject;
import com.yzzrjy.Service.UserService;
import com.yzzrjy.util.ResponseUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet(name = "PhoneLoginServlet", value = "/phoneLogin")
public class PhoneLoginServlet extends HttpServlet {
    private UserService userService = new UserService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String phone = request.getParameter("phone");
        if(phone == null || phone.length() == 0){
            ResponseUtils.parameterError(response);
            return;
        }

        try {
            JSONObject jsonObject = userService.phoneLogin(phone);
            ResponseUtils.success(response, jsonObject);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }catch (RuntimeException e){
            ResponseUtils.systemBusy(response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }



//    private int getNewId(QueryRunner queryRunner) throws SQLException {
//        long t = System.currentTimeMillis();
//        IdInfoBean idInfo = queryRunner.query("select * from id_info", new BeanHandler<>(IdInfoBean.class));
//        int newId = idInfo.getId() + idInfo.getDelta();
//        queryRunner.update("update id_info set id=?",newId);
//        System.out.println("时间2："+ (System.currentTimeMillis() - t));
//        return newId;
//    }
//    private boolean isBadId(int id){
//        String s = String.valueOf(id);
//        if(s.indexOf("44") != -1){
//            System.out.println("Bad String:------------------"+s);
//            return true;
//        }
//        return false;
//    }

}
